package com.ylyq.ylxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ylyq.ylxx.entity.YlyqCover;
import com.ylyq.ylxx.entity.YlyqCreativeAciton;
import com.ylyq.ylxx.entity.YlyqTypeProduct;
import com.ylyq.ylxx.mapper.YlyqCreativeAcitonMapper;
import com.ylyq.ylxx.mapper.YlyqTypeProductMapper;
import com.ylyq.ylxx.model.bean.GetProductResponse;
import com.ylyq.ylxx.model.bean.GetYlyqCreativeTag;
import com.ylyq.ylxx.model.request.DeleteProductRequest;
import com.ylyq.ylxx.model.request.UpdateProductRequest;
import com.ylyq.ylxx.service.IYlyqCreativeAcitonService;
import com.ylyq.ylxx.service.IYlyqTypeProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.websocket.OnClose;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品标识表。产品投放平台类别、产品名称类别 服务实现类
 * </p>
 *
 * @author xiaweichao
 * @since 2020-10-19
 */
@Service
public class YlyqTypeProductServiceImpl extends ServiceImpl<YlyqTypeProductMapper, YlyqTypeProduct> implements IYlyqTypeProductService {

    @Autowired
    private YlyqTypeProductMapper ylyqTypeProductMapper;

    @Autowired
    private YlyqCreativeAcitonMapper ylyqCreativeAcitonMapper;

    @Autowired
    private IYlyqCreativeAcitonService iYlyqCreativeAcitonService;

    /**
     * 功能描述：删除
     * @author 577
     * @param
     * @return
     * @date 2020/11/4
     **/
    @Override
    public int delete(DeleteProductRequest deleteProductRequest){
        if(deleteProductRequest.getCreativeTagId() != null){
            ylyqTypeProductMapper.deleteById(deleteProductRequest.getTypeProductId());
            QueryWrapper<YlyqCreativeAciton> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("product_id",deleteProductRequest.getTypeProductId());
            List<YlyqCreativeAciton> ylyqCreativeAcitons = ylyqCreativeAcitonMapper.selectList(queryWrapper);
            for(YlyqCreativeAciton ylyqCreativeAciton : ylyqCreativeAcitons){
                ylyqCreativeAcitonMapper.deleteById(ylyqCreativeAciton.getId());
            }
        }else if(deleteProductRequest.getTypeProductId() != null){
            return ylyqCreativeAcitonMapper.deleteById(deleteProductRequest.getCreativeTagId());
        }
        return 0;
    }

    /**
     * 功能描述：修改
     * @author 577
     * @param
     * @return
     * @date 2020/11/4
     **/
    @Override
    public int updateProduct(UpdateProductRequest updateProductRequest){
        YlyqTypeProduct ylyqTypeProduct = new YlyqTypeProduct();
        BeanUtils.copyProperties(updateProductRequest,ylyqTypeProduct);
        for(GetYlyqCreativeTag getYlyqCreativeTag : updateProductRequest.getCreativeTags()){
            YlyqCreativeAciton ylyqCreativeAciton = new YlyqCreativeAciton();
            BeanUtils.copyProperties(getYlyqCreativeTag,ylyqCreativeAciton);
            iYlyqCreativeAcitonService.saveOrUpdate(ylyqCreativeAciton);
        }
        return ylyqTypeProductMapper.updateById(ylyqTypeProduct);
    }



    /**
     * 功能描述：查询
     * @author 577
     * @param
     * @return
     * @date 2020/11/4
     **/
    @Override
    public List<GetProductResponse> selectList(String id){
        List<GetProductResponse> productResponses = new ArrayList<>();
        if(id != null){
            List<YlyqTypeProduct> ylyqTypeProducts = ylyqTypeProductMapper.selectList(null);
            for(YlyqTypeProduct ylyqTypeProduct : ylyqTypeProducts){
                GetProductResponse getProductResponse = new GetProductResponse();
                BeanUtils.copyProperties(ylyqTypeProduct,getProductResponse);
                QueryWrapper<YlyqCreativeAciton> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("product_id",ylyqTypeProduct.getId());
                List<YlyqCreativeAciton> ylyqCreativeAcitons = ylyqCreativeAcitonMapper.selectList(queryWrapper);
                List<GetYlyqCreativeTag> creativeTags = new ArrayList<>();
                for(YlyqCreativeAciton ylyqCreativeAciton : ylyqCreativeAcitons){
                    GetYlyqCreativeTag getYlyqCreativeTag = new GetYlyqCreativeTag();
                    BeanUtils.copyProperties(ylyqCreativeAciton,getYlyqCreativeTag);
                    creativeTags.add(getYlyqCreativeTag);
                }
                getProductResponse.setCreativeTags(creativeTags);
            }
        }else{
            YlyqTypeProduct ylyqTypeProduct = ylyqTypeProductMapper.selectById(id);
            GetProductResponse getProductResponse = new GetProductResponse();
            BeanUtils.copyProperties(ylyqTypeProduct,getProductResponse);
            QueryWrapper<YlyqCreativeAciton> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("product_id",ylyqTypeProduct.getId());
            List<YlyqCreativeAciton> ylyqCreativeAcitons = ylyqCreativeAcitonMapper.selectList(queryWrapper);
            List<GetYlyqCreativeTag> creativeTags = new ArrayList<>();
            for(YlyqCreativeAciton ylyqCreativeAciton : ylyqCreativeAcitons){
                GetYlyqCreativeTag getYlyqCreativeTag = new GetYlyqCreativeTag();
                BeanUtils.copyProperties(ylyqCreativeAciton,getYlyqCreativeTag);
                creativeTags.add(getYlyqCreativeTag);
            }
            getProductResponse.setCreativeTags(creativeTags);
        }
        return productResponses;
    }
}
